package com.mallcai.bigdata.ladon.service.peekdata.impl;

import com.mallcai.bigdata.ladon.common.SessionThreadLocal;
import com.mallcai.bigdata.ladon.dpl.entity.peekdata.PeekField;
import com.mallcai.bigdata.ladon.dpl.entity.system.User;
import com.mallcai.bigdata.ladon.dpl.mapper.peekdata.PeekFieldMapper;
import com.mallcai.bigdata.ladon.dpl.vo.AggFunction;
import com.mallcai.bigdata.ladon.dpl.vo.AnalysisType;
import com.mallcai.bigdata.ladon.dpl.vo.DataType;
import com.mallcai.bigdata.ladon.service.peekdata.IPeekFieldService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @author feixy
 * @version 1.0
 * @date 2019-07-08 17:01
 */
@Service
public class PeekFieldService implements IPeekFieldService {

    @Autowired
    private PeekFieldMapper peekFieldMapper;

    @Transactional
    @Override
    public int save(PeekField peekField){
        peekField.setType(StringUtils.isEmpty(peekField.getAggExpression())?
                AnalysisType.Dimension.getCode() : AnalysisType.Measure.getCode());
        peekField.setAggDataType(AggDataTypeUtils.calculateAggDataType(peekField));
        return  peekFieldMapper.insert(peekField);
    }

    @Transactional
    @Override
    public int deleteById(Long id){
        return peekFieldMapper.deleteById(id, SessionThreadLocal.getUserId());
    }

    @Transactional
    @Override
    public int deleteByPeekId(Long peekId){
        return peekFieldMapper.deleteByPeekId(peekId, SessionThreadLocal.getUserId());
    }

    @Override
    public List<PeekField> getByPeekId(Long peekId) {
        return peekFieldMapper.getByPeekId(peekId);
    }
}
